package com.rd.student.ruanjian.class3.gaochenxi.day0731;

import java.util.*;

public class TestRandom2 {
   public static void main(String[] args) {
      System.out.println("用数组方法：");
      Func();
      System.out.println("用hashSet方法：");
      Func2();
      System.out.println();
      System.out.println("用List方法");
      Func3();
      System.out.println();
      System.out.println("用数组方法：");
      Func4(20);
   }

   public static void Func() {
      int min = 0;
      int max = 50;
      int[] arr = new int[20];
      Random random = new Random();
      for (int i = 0; i < arr.length; i++) {
         int randoms = min + random.nextInt(max - min);
         arr[i] = randoms;
         for (int j = 0; j < i; j++) {
            if (arr[j] == arr[i]) {
               i--;
               break;
            }
         }
      }
      System.out.println(Arrays.toString(arr));
   }

   public static void Func2() {
      //用hashSet去重
      int min = 0;
      int max = 50;
      HashSet set = new HashSet();
      while (set.size() < 20) {
         Random random = new Random();
         int randoms = min + random.nextInt(max - min);
         set.add(randoms);
      }
      for (Object obj : set) {
         System.out.print(obj + " ");
      }
   }

   public static void Func3() {
      int min = 0;
      int max = 50;
      int[] arr = new int[20];
      ArrayList<Integer> list = new ArrayList<>(arr.length);
      for (int i = 0; i < arr.length; i++) {
         Random random = new Random();
         int randoms = min + random.nextInt(max - min);
         arr[i] = randoms;
         if (list.contains(arr[i])) {
            i--;
            continue;
         }
         list.add(arr[i]);
      }
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
   }

   //?
   public static void Func4(int n) {
      int min = 0;
      int max = 50;
      int[] indexArr = new int[n];
      int count = 0;
      for (int i = 0; i < n; i++) {
         Random random = new Random();
         int index = min + random.nextInt(max - min);
         boolean flag = false;
         for (int j = 0; j < count; j++) {
            if (index == indexArr[j]) {
               flag = true;
               break;
            }
         }
         if (!flag) {
            indexArr[count] = index;
            count++;
         }
         if (flag) {
            i--;
            continue;
         }
      }
      for (int i = 0; i < indexArr.length; i++) {
         System.out.print(indexArr[i] + " ");
      }
   }

   public static void Func5() {
      int min = 0;
      int max = 50;
      int[] arr = new int[50];
      LinkedList linkedList = new LinkedList();
      for (int i = 0; i < arr.length; i++) {
         arr[i] = i + 1;
      }
      for (int i = 0; i < 20; i++) {
         Random random=new Random();
         int index =min+random.nextInt(max-min);
         if(arr[index]!=-1){
            linkedList.add(arr[index]);
         }
         arr[index]=-1;
      }
      System.out.println(linkedList);
   }
}
